<template>
	<view class="">
		<view class="beijin">
			<image
				src="http://fangguaner.oss-cn-chengdu.aliyuncs.com/upload/20210721/fb81077df717acda561a8d3a5e32198c.png"
				mode=""></image>
		</view>
		<view class="box">
			<view class="toubu">
				<view class="time">
					<image
						src="http://fangguaner.oss-cn-chengdu.aliyuncs.com/upload/20210721/6dd5d5ed3527a6e306dd7123c243ddf7.png"
						mode=""></image>
					<text class="residuetime">剩余时间：{{remaining}}</text>
				</view>
				<view class="percentage">{{index+1}}/{{objarr.length}}</view>
			</view>
			<view class="timu">
				{{objarr[index].problem}}
			</view>
			<view class="daan" v-for="(items,indexs) in objarr[index].answer" :key="indexs">
				<van-checkbox-group :value="objarr[index].result"
					@change="onChange($event, objarr[index].right_key.length==1?false:true)">
					<van-checkbox :name="items" checked-color="#ff6600" icon-size="25px">
						<text class="ti">{{items}}</text>
					</van-checkbox>
				</van-checkbox-group>
			</view>
			<view class="btnbox">
				<van-button round type="info" color="#fc7b63" @click="up" size="normal" :disabled="index==0?true:''">
					上一页</van-button>
				<van-button round type="info" color="#fc7b63" @click="down" v-if='index<objarr.length-1' size="normal">
					下一页</van-button>
				<van-button round type="info" color="#fc7b63" @click="submit" v-if='index==objarr.length-1'
					size="normal">提交答案</van-button>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getbuyquestion,
		Submittheanswer
	} from "../../api/api/index"
	export default {
		data() {
			return {
				result: [],
				selectarr: [],
				stime: "00",
				arr: [{
					name: "a"
				}, {
					name: "b"
				}, {
					name: "c"
				}],
				objarr: [],
				examination: {},
				index: 0,
				id: 0,
				remaining: '00:00:00', //显示剩余时间
				remainingd: '' //数据返回时间秒
			}
		},
		methods: {
			//flag true是多少 false是单选
			onChange(e, flag) {
				let a = e.detail
				if (flag) {
					this.objarr[this.index].result = a
				} else {
					if (this.objarr[this.index].result.length == 0) {
						this.objarr[this.index].result = a
					} else {
						let b = this.objarr[this.index].result[0] //已选择的一个i
						let index = a.indexOf(b)
						if (index > -1) {
							a.splice(index, 1)
							this.objarr[this.index].result = a
						}
					}
				}
			},
			up() {
				this.index--
			},
			down() {
				this.index++
			},
			submit() {
				let pass = this.objarr.map(v=>{
					let a = v.result.map(k=>{
						return k[0]
					})
					return {
						id: v.id,
						answer: a.join(',')
					}
				})
				let data={type_id:this.id,data:pass}
				Submittheanswer(data).then(res=>{
					uni.showToast({
						title:res.message,
						icon:"none",
						duration:1500
					})
					setTimeout(function(){
						this.jumplink({link:"/pages/grade/index",query:{id:this.id}})
					},1500)
				})
			},
			pubilc() {
				let index = this.index
				getbuyquestion({
					id: this.id
				}).then(res => {
					this.objarr = res.data.map(v => {
						if (v.answer.length == 1) {
							v.result = ''
						} else {
							v.result = []
						}
						return v
					})
				})
			},
			//得到的秒换算成时分秒
			djs: function(ms) {
				var h = parseInt(ms / (60 * 60));
				var m = parseInt((ms % (60 * 60)) / 60);
				var s = (ms % (60 * 60)) % 60;
				return h + ":" + m + ":" + s;
			},
			//倒计时计时器
			jishiqi: function() {
				var dj = this.remainingd;
				if (dj <= 0) {
					this.remaining = "已过期";
				} else {
					var ddf = this.djs(dj); //格式化过后的时间
					this.remaining = ddf;
					//当前时间减去时间
					dj = dj - 1;
					this.remainingd = dj;
				}

			},
		},
		onShow() {
			this.pubilc()
		},
		onLoad(e) {
			this.id = e.id
			this.remainingd = e.time * 60
			let that = this
			setInterval(function() {
				that.jishiqi()
			}, 1000)
		}
	}
</script>

<style scoped lang="less">
	.beijin {
		image {
			width: 100%;
			margin-top: -180rpx;
		}
	}
	.box {
		width: 690rpx;
		height: 1142rpx;
		background-color: #ffffff;
		box-shadow: 0rpx 2rpx 5rpx 1rpx rgba(0, 0, 0, 0.21);
		border-radius: 20rpx;
		position: absolute;
		top: 30rpx;
		left: 30rpx;

		.toubu {
			width: 690rpx;
			height: 100rpx;
			background-color: #fff9ca;
			border-top-left-radius: 20rpx;
			border-top-right-radius: 20rpx;
			display: flex;
			justify-content: space-between;
			line-height: 100rpx;
			padding: 0 30rpx;
			box-sizing: border-box;
			font-family: MicrosoftYaHei;
			font-size: 22rpx;
			color: #ff7e00;

			.time {
				display: flex;
				align-items: center;

				image {
					width: 26rpx;
					height: 25rpx;
					margin-right: 10rpx;
				}
			}
		}

		.timu {
			width: 619rpx;
			font-family: MicrosoftYaHei-Bold;
			font-size: 30rpx;
			line-height: 50rpx;
			color: #000000;
			margin: 52rpx auto;
			font-weight: 700;
		}

		.daan {
			width: 650rpx;
			height: 90rpx;
			background-color: #ffffff;
			border-radius: 5rpx;
			border: solid 1rpx #999999;
			opacity: 0.5;
			margin: 10rpx auto;
			padding: 20rpx;
			box-sizing: border-box;

			.ti {
				font-family: MicrosoftYaHei;
				font-size: 26rpx;
				color: #000000;
			}
		}

		.btnbox {
			display: flex;
			justify-content: space-between;
			margin: 270rpx 20rpx;
		}
	}
</style>
